//
//  GradeChooseView.m
//  Learn
//
//  Created by 迟建国 on 2024/4/5.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "GradeChooseView.h"
#import "HomeRequest.h"

@interface GradeChooseView ()<UIPickerViewDataSource, UIPickerViewDelegate>
@property (nonatomic, strong) HSchildrenBookModel *secondmodel;
@property (nonatomic, strong) UIPickerView *pickerView;
@property (nonatomic, strong) NSArray *firstColumnData;
@property (nonatomic, strong) NSArray *secondColumnData;

@end

@implementation GradeChooseView

+ (GradeChooseView *)chooseGradeWithBooks:(NSArray *)books Complection:(void(^)(HSchildrenBookModel *model))block {
    GradeChooseView *  inputView;
    inputView = [[GradeChooseView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 335 + SafeAreaBottomHeight)];
    inputView.firstColumnData = books;
    if (books.count != 0) {
        HSTextBookModel *model = books.firstObject;
        inputView.secondColumnData = model.children;
        if (inputView.secondColumnData.count != 0) {
            HSchildrenBookModel *secondModel = inputView.secondColumnData.firstObject;
            inputView.secondmodel = secondModel;
        }
    }

    [inputView initUI];
    inputView.chooseBlock = block;
    
    NSLog(@"%@", NSObject.getKeyWindowRootViewController);
    [NSObject.getKeyWindowRootViewController gq_presentFromeBottomView:inputView];
    NSObject.getKeyWindowRootViewController.gq_enableBlankDismiss = YES;
    
    return inputView;
}
 
- (void)initUI {
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 335 + SafeAreaBottomHeight)];
    bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:bgView];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:rgba(51, 51, 51, 1) forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:16];
    [cancelBtn addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left).offset(5);
        make.top.equalTo(bgView.mas_top).offset(5);
        make.height.offset(42);
        make.width.offset(52);
    }];
    
    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [sureBtn setTitleColor:rgba(12, 183, 108, 1) forState:UIControlStateNormal];
    sureBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:16];
    [sureBtn addTarget:self action:@selector(sure) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:sureBtn];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(bgView.mas_right).offset(-5);
        make.top.equalTo(bgView.mas_top).offset(5);
        make.height.offset(42);
        make.width.offset(52);
    }];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = rgba(229, 229, 229, 1);
    [bgView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(bgView);
        make.top.equalTo(bgView.mas_top).offset(52);
        make.height.offset(1);
    }];
    
    // 创建 UIPickerView
    self.pickerView = [[UIPickerView alloc] init];
    self.pickerView.dataSource = self;
    self.pickerView.delegate = self;
    self.pickerView.showsSelectionIndicator = NO;
    [bgView addSubview:self.pickerView];
    [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(bgView);
        make.top.equalTo(bgView.mas_top).offset(53);
        make.bottom.equalTo(bgView.mas_bottom).offset(-SafeAreaBottomHeight - 25);
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        UIView *cover = [self.pickerView.subviews objectAtIndex:1];
        [cover setBackgroundColor:lClearColor];
        double x = (lScreenWidth - cover.width)/2.0;
        [cover addSubview:separateLineWithColor(CGRectMake(-x, 0, lScreenWidth, 0.5), rgba(229, 229, 229, 1))];
        [cover addSubview:separateLineWithColor(CGRectMake(-x, cover.height - 0.5, lScreenWidth, 0.5), rgba(229, 229, 229, 1))];
    });
    
}

#pragma mark - UIPickerViewDataSource
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    if (component == 0) {
        return lScreenWidth/3.000;
    } else {
        return lScreenWidth*2/3.000;
    }
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2; // 两列
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return self.firstColumnData.count;
    } else {
        return self.secondColumnData.count;
    }
}

#pragma mark - UIPickerViewDelegate

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    
    if (component == 0) {
        HSTextBookModel *model = self.firstColumnData[row];
        return model.gradeName2;
    } else {
        HSTextBookModel *secondmodel = self.secondColumnData[row];
        return secondmodel.versionName;
    }
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view;
{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.font= MFontWithSize(16);
        pickerLabel.textColor = rgba(51, 51, 51, 1);
        pickerLabel.textAlignment= NSTextAlignmentCenter;
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
    }
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    //在该代理方法里添加以下两行代码删掉上下的黑线
   
//    [[pickerView.subviews objectAtIndex:2] setHidden:TRUE];
    return pickerLabel;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    // 处理选择事件
    if (component == 0) {
        NSLog(@"第一列选择了 %@", self.firstColumnData[row]);
        HSTextBookModel *model = self.firstColumnData[row];
        self.secondColumnData = [NSMutableArray arrayWithArray:model.children];
        if (self.secondColumnData.count != 0) {
            HSchildrenBookModel *secondModel = self.secondColumnData.firstObject;
            self.secondmodel = secondModel;
        }
        [self.pickerView reloadAllComponents];
    } else {
        HSchildrenBookModel *secondmodel = self.secondColumnData[row];
        self.secondmodel = secondmodel;
        NSLog(@"第二列选择了 %@", secondmodel.versionId);
    }
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 52;
}
- (void)cancel {
    [NSObject.getKeyWindowRootViewController gq_removeBgView];
}
- (void)sure {
    self.chooseBlock(self.secondmodel);
    [NSObject.getKeyWindowRootViewController gq_removeBgView];
}

@end
